/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.h2t.loveplace.list;

import com.h2t.loveplace.models.Place;
import com.h2t.loveplace.utils.Storage;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import java.io.IOException;

/**
 *
 * @author QuyenPac
 */
public class PlaceListRenderer extends Container implements ListCellRenderer {

    private Image defaultImage;
    private Label imgLabel;
    private Label title, focus;

    public PlaceListRenderer() {
        title = new Label();

        focus = new Label();
        focus.getStyle().setBgTransparency(100);
        focus.getStyle().setBgColor(0xFFFFFF);
        try {
            defaultImage = Image.createImage("/photo/appicon.png").scaled(50, 50);
            imgLabel = new Label(defaultImage);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        imgLabel.getStyle().setMargin(4, 4, 4, 4);
        imgLabel.getStyle().setBgTransparency(100);
        setLayout(new BoxLayout(BoxLayout.X_AXIS));
        title.getStyle().setFont(Font.createSystemFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        title.getStyle().setBgTransparency(100);
        addComponent(imgLabel);
        addComponent(title);
    }

    public Component getListCellRendererComponent(List list, Object o, int i, boolean bln) {
        Place model = (Place) o;
        if (model != null && model.getImg() != null && model.getImg()!= "") {
            try {
                if(model.getImg().startsWith("file")) {
                   
                    Image img = Storage.loadImg(model.getImg());
                    if(img == null) {
                        this.imgLabel.setIcon(defaultImage);
                        System.out.println("NULLLL IMG");
                    }
                    else {
                        this.imgLabel.setIcon(img);
                    }
                    img.scaled(50, 50);
                     
                }
                else {
                    this.imgLabel.setIcon(Image.createImage(model.getImg()).scaled(50, 50));
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            this.imgLabel.setIcon(defaultImage);
        }
        this.title.setText(model.getTitle());
        this.getStyle().setBorder(Border.createLineBorder(1, 0x666666));
        return this;
    }

    public Component getListFocusComponent(List list) {

        return focus;
    }
}
